<?php
// @formatter:off
/**
 * @file WCParserBehaviorOVER.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-11 21:02:48 +0000 (Thu, 11 Jul 2013) $
 *
 * $Id: WCParserBehaviorOVER.php 95 2013-07-11 21:02:48Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/branches/wcomix-1.0/includes/parser/WCParserBehaviorOVER.php $
 */
// @formatter:on

//
// Loading father class.
require_once dirname(__FILE__)."/WCParserBehavior.php";

//
// Adding the function to evaluate by this behavior.
$wcParserBehaviorMatrix["OVER"] = "WCParserBehaviorOVER";

/**
 * @class WCParserBehaviorOVER This is a specific behavior that parses the
 * contents of its node as many times a the number of elements of an array
 * specified the the parameter 'list'. When it's used it looks like:
 * @code
 * <:OVER:list=array-name:/>contents<::/>
 * @endcode
 */
class WCParserBehaviorOVER extends WCParserBehavior {
	/**
	 * @var string This is the type of warnings used when a required variable
	 * is not present.
	 */
	const WarningNONAME = "OVER_NOVARIABLE";
	/**
	 * @var string This is the type of warnings used when a variable has to be
	 * an array and it's not.
	 */
	const WarningNOTARRAY = "OVER_NOTARRAY";
	/**
	 * @var string This is the type of warnings used when a required parameter
	 * is not present.
	 */
	const WarningNOPARAM = "OVER_NOPARAM";
	/**
	 * This method perform a check of the object, sets the current status
	 * (variable $this->_ok) and return it.
	 *
	 * @return boolean Returns the current status after a check.
	 */
	public function check() {
		//
		// An OVER behavior must belong to a parser node.
		if($this->_node) {
			//
			// At this point, everything checks out.
			$this->_ok = true;
			$this->_errorMsg = "Ok";
		} else {
			//
			// Behavior's node is not present. Error message is set
			// and the internal status becomes false.
			$this->_errorMsg = "OVER node is not set";
			$this->_ok = false;
		}
		//
		// Returning current object status.
		return $this->isOk();
	}
	public function get() {
		//
		// Setting the default value to be returned.
		$out = false;
		//
		// OVER condition will only be evaluated when this behavior has
		// no issues.
		if($this->isOk()) {
			//
			// Loading parameters.
			$params = $this->_node->params();
			//
			// Checking the presence of required parameter 'list'.
			if(isset($params["list"])) {
				//
				// Creating a default array walk over.
				$over = array();
				//
				// Creating a shortcut for the array's name.
				$overArray = $params["list"];
				//
				// Checking that the list exists and it's an
				// array.
				if($this->_parser->has($overArray) && is_array($this->_parser->$overArray)) {
					//
					// Getting the array and setting the list
					// of steps to walk.
					$over = $this->_parser->$overArray;
				} elseif(!$this->_parser->has($overArray)) {
					//
					// The variable name set is not valid,
					// therefore, a warning message is added.
					$this->_parser->setWarning(WCParserBehaviorOVER::WarningNONAME, "Variable '{$overArray}' is not present", false, false, __CLASS__);
				} else {
					//
					// The variable name set is not an array,
					// therefore, a warning message is added.
					$this->_parser->setWarning(WCParserBehaviorOVER::WarningNOTARRAY, "Variable '{$overArray}' is not an array", false, false, __CLASS__);
				}
				//
				// Creating a backup of current variables.
				$currVariables = $this->_parser->variables();
				//
				// If parameter 'key' is present, it means the
				// list of steps is not an array of arrays and it
				// must be treated in a more simple way.
				// From here, $simpleWay will hold a variable name.
				$simpleWay = isset($params["key"]) ? $params["key"] : false;
				//
				// Setting a default value for results.
				$out = "";
				//
				// Performing each step.
				foreach($over as $key => $extraValues) {
					//
					// Mode check.
					if($simpleWay) {
						//
						// In simple way mode, keys from
						// the steps specification array
						// are ignored and values are
						// assigned on each step to
						// variable given by the parameter
						// 'key'.
						$this->_parser->assign($simpleWay, $extraValues);
					} else {
						//
						// In normal mode, the value of a
						// step is considered as an
						// associative array which
						// specifies variable to use when
						// parsing contents.
						$this->_parser->massiveAssign($extraValues);
					}
					//
					// Parsing contents of the node and
					// appending them to results.
					$out .= $this->_node->parseChildren();
					//
					// Restoring variables from backup.
					$this->_parser->massiveAssign($currVariables, true);
				}
			} else {
				//
				// There's no variable name set, therefore, a
				// warning message is added.
				$this->_parser->setWarning(WCParserBehaviorOVER::WarningNOPARAM, "Parameter 'list' is not present", false, false, __CLASS__);
			}
		}
		//
		// Returning evaluation results.
		return $out;
	}
	/**
	 * This method redefines the inherited class initializer.
	 */
	protected function init() {
		//
		// Setting a type of warnings used when a required variable is not
		// present.
		$this->_parser->setWarning(WCParserBehaviorOVER::WarningNONAME, false, false, "OVER: variable name belongs to no variable");
		//
		// Setting a type of warnings used when a variable has to be an
		// array and it's not.
		$this->_parser->setWarning(WCParserBehaviorOVER::WarningNOTARRAY, false, false, "OVER: variable is not an array");
		//
		// Setting a type of warnings used when a required parameter is
		// not present.
		$this->_parser->setWarning(WCParserBehaviorOVER::WarningNOPARAM, false, false, "OVER: a requiered parameter is not present");

	}
}
?>